# while循环实现
def function_by_while():
    # while循环，第1种形式
    x = 1
    while x < 10:
        y = 1
        while y <= x:
            print('{0}x{1}={2}'.format(y, x, y * x), end='\t')
            y = y + 1
        x = x + 1
        print()
    line()
    # while循环，第2种形式
    x = 9
    while x > 0:
        y = 1
        while y <= x:
            print('{0}x{1}={2}'.format(y, x, y * x), end='\t')
            y = y + 1
        x = x - 1
        print()
    line()
    # while循环，第3种形式
    x = 1
    while x < 10:
        y = x
        print(''.rjust((9 - x) * 2, '\t'), end='')
        while y > 0:
            print('{0}x{1}={2}'.format(y, x, y * x), end='\t')
            y = y - 1
        x = x + 1
        print()
    line()
    # while循环，第4种形式
    x = 9
    while x > 0:
        y = x
        print(''.rjust((9 - x) * 2, '\t'), end='')
        while y > 0:
            print('{0}x{1}={2}'.format(y, x, y * x), end='\t')
            y = y - 1
        x = x - 1
        print()
    line()


# for循环实现
def function_by_for():
    # for循环，第1种形式
    arr_x = range(1, 10)
    for x in arr_x:
        arr_y = range(1, x + 1)
        for y in arr_y:
            print('{0}x{1}={2}'.format(y, x, y * x), end='\t')
        print()
    line()
    # for循环，第2种形式
    arr_x = range(9, 0, -1)
    for x in arr_x:
        arr_y = range(1, x + 1)
        for y in arr_y:
            print('{0}x{1}={2}'.format(y, x, y * x), end='\t')
        print()
    line()
    # for循环，第3种形式
    arr_x = range(1, 10)
    for x in arr_x:
        arr_y = range(x, 0, -1)
        print(''.rjust((9 - x) * 2, '\t'), end='')
        for y in arr_y:
            print('{0}x{1}={2}'.format(y, x, y * x), end='\t')
        print()
    line()
    # for循环，第4种形式
    arr_x = range(9, 0, -1)
    for x in arr_x:
        arr_y = range(x, 0, -1)
        print(''.rjust((9 - x) * 2, '\t'), end='')
        for y in arr_y:
            print('{0}x{1}={2}'.format(y, x, y * x), end='\t')
        print()
    line()


def line():
    print('=' * 70)


if __name__ == '__main__':
    function_by_while()
    function_by_for()
